<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="120px">
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['business:electricityPrice:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['business:electricityPrice:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['business:electricityPrice:remove']">删除</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:electricityPrice:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="electricityPriceList" @selection-change="handleSelectionChange"
            @expand-change="handleExpandChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-table v-loading="loading" :data="props.row.tableData">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="光伏电站电价ID" align="center" prop="priceId" />
                        <el-table-column label="光伏电站ID" align="center" prop="pvId" />
                        <el-table-column label="电价类型" align="center" prop="electricityPriceType">
                            <template slot-scope="scope">
                                <dict-tag :options="dict.type.pv_station_electricity_price_class"
                                    :value="scope.row.electricityPriceType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="开始日期" align="center" prop="startDate" />
                        <el-table-column label="结束日期" align="center" prop="endDate" />
                        <el-table-column label="排序号" align="center" prop="sortNo" />
                    </el-table>
                </template>
            </el-table-column>
            <el-table-column label="光伏电站" align="center" prop="pvId" />
            <el-table-column label="用电电价类型" align="center" prop="electricityPriceType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.pv_station_electricity_price_class"
                        :value="scope.row.electricityPriceType" />
                </template>
            </el-table-column>
            <el-table-column label="用电电价" align="center" prop="electricityPrice">
                <template slot-scope="scope">
                    {{ scope.row.electricityPrice }}元/kWh
                </template>
            </el-table-column>
            <el-table-column label="上网电价类型" align="center" prop="gridElectricityPriceType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.pv_station_electricity_price_class"
                        :value="scope.row.gridElectricityPriceType" />
                </template>
            </el-table-column>
            <el-table-column label="上网电价" align="center" prop="gridElectricityPrice">
                <template slot-scope="scope">
                    {{ scope.row.gridElectricityPrice }}元/kWh
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:electricityPrice:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:electricityPrice:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改光伏电站电价信息主对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="用电电价类型" prop="electricityPriceType">
                            <el-radio-group v-model="form.electricityPriceType">
                                <el-radio-button v-for="dict in dict.type.pv_station_electricity_price_class"
                                    :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-show="form.electricityPriceType == '10'">
                        <el-form-item label="用电电价" prop="electricityPrice">
                            <el-input-number v-model="form.electricityPrice" placeholder="请输入用电电价，单位元/kWh"
                                :precision="2" :step="0.1" :max="90" style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.electricityPriceType == '11'">
                        <ElectricityPriceTable @getElectricityPriceList="getElectricityPriceList" />
                    </el-col>
                    <el-col :span="24">
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="上网电价类型" prop="gridElectricityPriceType">
                            <el-radio-group v-model="form.gridElectricityPriceType">
                                <el-radio-button v-for="dict in dict.type.pv_station_electricity_price_class"
                                    :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-show="form.gridElectricityPriceType == '10'">
                        <el-form-item label="上网电价" prop="gridElectricityPrice">
                            <el-input-number v-model="form.gridElectricityPrice" placeholder="请输入上网电价，单位元/kWh"
                                :precision="2" :step="0.1" :max="90" style="width: 100%;" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.gridElectricityPriceType == '11'">
                        <GridElectricityPriceTable @getGridElectricityPriceList="getGridElectricityPriceList" />
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listElectricityPrice, getElectricityPrice, delElectricityPrice, addElectricityPrice, updateElectricityPrice } from "@/api/business/electricityPrice"
import ElectricityPriceTable from "./electricityPriceTable.vue";
import GridElectricityPriceTable from "./gridElectricityPriceTable.vue";
export default {
    name: "ElectricityPrice",
    dicts: ['pv_station_electricity_price_class'],
    components: { ElectricityPriceTable, GridElectricityPriceTable },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 光伏电站电价信息主表格数据
            electricityPriceList: [],
            // 光伏电站电价信息详情表格数据
            electricityPriceDetailList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                pvId: null,
                electricityPriceType: null,
                electricityPrice: null,
                gridElectricityPriceType: null,
                gridElectricityPrice: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                pvId: [
                    { required: true, message: "光伏电站ID不能为空", trigger: "blur" }
                ],
                electricityPriceType: [
                    { required: true, message: "用电电价类型不能为空", trigger: "change" }
                ],
                gridElectricityPriceType: [
                    { required: true, message: "上网电价类型不能为空", trigger: "change" }
                ],
                createBy: [
                    { required: true, message: "创建人不能为空", trigger: "blur" }
                ],
                createTime: [
                    { required: true, message: "创建时间不能为空", trigger: "blur" }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询光伏电站电价信息主列表 */
        getList() {
            this.loading = true
            listElectricityPrice(this.queryParams).then(response => {
                this.electricityPriceList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        handleExpandChange(row, expandedRows) {
            const isExpand = expandedRows.includes(row);
            if (!isExpand) return;
            // 真实接口请求（示例）
            getElectricityPrice(row.priceId) // 传当前行的rateId
                .then((response) => {
                    console.log(response.data, "response.data");
                    row.tableData = response.data; // 接口返回的详情数据
                    this.loadingInfo = false; // 标记加载完成
                })
                .catch((error) => {
                    this.$message.error("详情加载失败，请重试");
                    console.error(error);
                });
        },
        // 获取用电电价分段列表数据
        getElectricityPriceList(data) {
            console.log(data);
            this.form.params.electricityPriceList = data;
        },
        getGridElectricityPriceList(data) {
            console.log(data);
            this.form.params.gridElectricityPriceList = data;
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                priceId: null,
                pvId: null,
                electricityPriceType: '10',
                electricityPrice: null,
                gridElectricityPriceType: '10',
                gridElectricityPrice: null,
                updateBy: null,
                updateTime: null,
                createBy: null,
                createTime: null,
                params: {
                    gridElectricityPriceClass: null,
                    gridElectricityPrice: null,
                    gridElectricityPriceList: null,
                    electricityPriceClass: null,
                    electricityPrice: null,
                    electricityPriceList: null,
                }
            }
            this.resetForm("form")
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.priceId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加光伏电站电价信息主"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const priceId = row.priceId || this.ids
            getElectricityPrice(priceId).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改光伏电站电价信息主"
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.form.params.electricityPriceClass = this.form.electricityPriceType;
            this.form.params.gridElectricityPriceClass = this.form.gridElectricityPriceType;
            this.form.params.electricityPrice = this.form.electricityPrice;
            this.form.params.gridElectricityPrice = this.form.gridElectricityPrice;
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.priceId != null) {
                        updateElectricityPrice(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addElectricityPrice(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const priceIds = row.priceId || this.ids
            this.$modal.confirm('是否确认删除光伏电站电价信息主编号为"' + priceIds + '"的数据项？').then(function () {
                return delElectricityPrice(priceIds)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/electricityPrice/export', {
                ...this.queryParams
            }, `electricityPrice_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
